package edu.zjut.hjg.saas.biz.service.impl;

import java.util.List;

import edu.zjut.hjg.saas.biz.dao.BabyDao;
import edu.zjut.hjg.saas.biz.dao.SchoolClassDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.Baby;
import edu.zjut.hjg.saas.biz.model.SchoolClass;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.BabyService;
import edu.zjut.hjg.saas.biz.support.OperateType;
import edu.zjut.hjg.saas.biz.support.Paginated;

public class BabyServiceImpl implements BabyService {

    private UserLogDao     userLogDao;
    private BabyDao        babyDao;
    private SchoolClassDao schoolClassDao;

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    public void setBabyDao(BabyDao babyDao) {
        this.babyDao = babyDao;
    }

    public void setSchoolClassDao(SchoolClassDao schoolClassDao) {
        this.schoolClassDao = schoolClassDao;
    }

    @Override
    public Integer create(Baby baby, String operator) {
        if (baby.getSchoolClass().getId().intValue() == -1) {
            // 创建默认班级
            SchoolClass schoolClass = new SchoolClass();
            schoolClass.setKindergarten(baby.getKindergarten());
            schoolClass.setName("默认班级");
            schoolClassDao.create(schoolClass);
            baby.setSchoolClass(schoolClass);
        }
        Integer id = babyDao.create(baby);
        UserLog userLog = new UserLog();
        userLog.setKindergarten(baby.getKindergarten());
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.INSERT);
        userLog.setDescription("添加了一个宝宝名单:" + baby.getName());
        userLogDao.create(userLog);
        return id;
    }

    @Override
    public void deleteByIdAndAppId(int id, int appId, String operator) {
        Baby baby = babyDao.findByIdAndAppId(id, appId);
        if (baby == null) {
            throw new RuntimeException("no baby found for id=" + id + " and appId=" + appId);
        }
        babyDao.deleteByIdAndAppId(id, appId);
        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.DELETE);
        userLog.setDescription("删除了一个宝宝名单:" + baby.getName());
        userLogDao.create(userLog);
    }

    @Override
    public Baby findByIdAndAppId(int id, int appId) {
        Baby baby = babyDao.load(id);
        if (baby == null) {
            throw new RuntimeException("no baby found for id=" + id + " and appId=" + appId);
        }
        return baby;
    }

    @Override
    public Paginated<Baby> listByAppId(int appId, int pageSize, int page) {
        return babyDao.listByAppId(appId, pageSize, page);
    }

    @Override
    public void update(Baby baby, int appId, String operator) {
        babyDao.update(baby);
        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.UPDATE);
        userLog.setDescription("修改了一个宝宝名单:" + baby.getName());
        userLogDao.create(userLog);
    }

    @Override
    public List<Baby> listAllByAppId(int appId) {
        return babyDao.listAllByAppId(appId);
    }
}
